package com.gpnu7_412.yanzhi.controller.handing;

import com.github.pagehelper.PageInfo;
import com.gpnu7_412.yanzhi.entity.CommentHanding;
import com.gpnu7_412.yanzhi.entity.DynamicHanding;
import com.gpnu7_412.yanzhi.entity.Handing;
import com.gpnu7_412.yanzhi.entity.UserHanding;
import com.gpnu7_412.yanzhi.exception.custom.RequestDataException;
import com.gpnu7_412.yanzhi.service.HandingService;
import com.gpnu7_412.yanzhi.service.UserService;
import com.gpnu7_412.yanzhi.status.Impl.ResultCode;
import com.gpnu7_412.yanzhi.util.Result;
import com.gpnu7_412.yanzhi.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZZH
 * @author HJJ
 */
@CrossOrigin
@RestController
@RequestMapping("/handing")
public class HandingController {
    @Autowired
    HandingService handingService;
    @Autowired
    UserService userService;

    /**
     * 获取所有用户举报
     * @return 用户举报列表
     */
    @GetMapping("/user/all")
    public Result getAllUserHanding(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageInfo<UserHanding> userHandingPageInfo = handingService.getAllUserHanding(pageNum, pageSize);
        return new Result(userHandingPageInfo);
    }

    /**
     * 获取所有动态举报
     * @return 动态举报列表
     */
    @GetMapping("/dynamic/all")
    public Result getAllDynamicHanding(
        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
            PageInfo<DynamicHanding> dynamicHandingPageInfo = handingService.getAllDynamicHanding(pageNum, pageSize);
            return new Result(dynamicHandingPageInfo);
    }

    /**
     * 获取所有评论举报
     * @return 评论举报列表
     */
    @GetMapping("/comment/all")
    public Result getAllCommentHanding(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageInfo<CommentHanding> commentHandingPageInfo = handingService.getAllCommentHanding(pageNum, pageSize);
        return new Result(commentHandingPageInfo);
    }

    /**
     * 同意用户举报信息
     * @param handingId 举报ID
     * @return 响应对象
     */
    @PostMapping("/user/agree")
    public Result agreeUserHanding(@RequestParam("handingId") Integer handingId) {
        handingService.agreeUserHanding(handingId);
        return new Result();
    }

    /**
     * 同意动态举报信息
     * @param handingId 举报ID
     * @return 响应对象
     */
    @PostMapping("/dynamic/agree")
    public Result agreeDynamicHanding(@RequestParam("handingId") Integer handingId) {
        handingService.agreeDynamicHanding(handingId);
        return new Result();
    }

    /**
     * 同意评论举报信息
     * @param handingId 举报ID
     * @return 响应对象
     */
    @PostMapping("/comment/agree")
    public Result agreeCommentHanding(@RequestParam("handingId") Integer handingId) {
        handingService.agreeCommentHanding(handingId);
        return new Result();
    }

    /**
     * 删除用户举报信息
     * @param handingId 举报ID
     * @return 响应对象
     */
    @DeleteMapping("/user/delete")
    public Result deleteUserHanding(@RequestParam("handingId") Integer handingId) {
        if (!handingService.disagreeUserHanding(handingId)) {
            throw new RequestDataException("请求参数错误");
        }
        return new Result();
    }

    /**
     * 删除动态举报信息
     * @param handingId 举报ID
     * @return 响应对象
     */
    @DeleteMapping("/dynamic/delete")
    public Result deleteDynamicHanding(@RequestParam("handingId") Integer handingId) {
        if (!handingService.disagreeDynamicHanding(handingId)) {
            throw new RequestDataException("请求参数错误");
        }
        return new Result();
    }

    /**
     * 删除评论举报信息
     * @param handingId 举报ID
     * @return 响应对象
     */
    @DeleteMapping("/comment/delete")
    public Result deleteCommentHanding(@RequestParam("handingId") Integer handingId) {
        if (!handingService.disagreeCommentHanding(handingId)) {
            throw new RequestDataException("请求参数错误");
        }
        return new Result();
    }



    // 获取所有代办信息

    /**
     * @deprecated
     * @return
     */
    @GetMapping("")
    public Result getAllHanding() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String permission= (String) map.get("permission");
        if (permission.equals("用户")) {
            return new Result(ResultCode.SUCCESS, "您没有访问的权限");
        }
        List<Handing> handingList = handingService.getHandingList();
        Map<String, Object> result = new HashMap<>(2);
        result.put("totalCount", handingList.size());
        result.put("list", handingList);
        return new Result(ResultCode.SUCCESS, "获取成功", result);
    }

    /**
     * @deprecated
     * @param handingMap
     * @return
     */
    // 处理代办信息
    @PostMapping("/handle")
    public Result handleHanding(@RequestBody Map<String, Object> handingMap) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String permission= (String) map.get("permission");
        if (permission.equals("用户")) {
            return new Result(ResultCode.SUCCESS, "您没有访问的权限");
        }
        Handing handing = new Handing();
        handing.setUserId((Integer) handingMap.get("userId"));
        handing.setCommentId((Integer) handingMap.get("commentId"));
        handing.setDynamicId((Integer) handingMap.get("dynamicId"));
        handing.setType((String) handingMap.get("type"));
        handing.setContent((String) handingMap.get("content"));
        handingService.handleHanding((String)handingMap.get("handle"), handing);

        return new Result(ResultCode.SUCCESS, "处理成功");
    }
}
